//
//  XFStatusCell.swift
//  mxfweibo
//
//  Created by apple1 on 15/12/20.
//  Copyright © 2015年 莫祥富. All rights reserved.
//

import UIKit

class XFStatusCell: UITableViewCell {
    
    //MARK: - 属性
    var pictureViewWidthCon : NSLayoutConstraint?
    
    var pictureViewHeightCon : NSLayoutConstraint?
    
    var status: XFStatus? {
        didSet {
            topView.status = status
            
            contentLabel.text = status?.text
            
            pictureView.status = status
            
            let size = pictureView.calculateViewSize()
            
            pictureViewHeightCon?.constant = size.height
            pictureViewWidthCon?.constant = size.width
            
        }
    }
    
    //MARK: - 计算行高
    func rowHeight(status:XFStatus) -> CGFloat {
        
        self.status = status
        
        layoutIfNeeded()
        
        let maxY = CGRectGetMaxY(bottomView.frame)
        
        return maxY
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
        
    }
    //MARK: - 准备UI
    func prepareUI() {
        
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(bottomView)
        contentView.addSubview(pictureView)
        
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size:CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size:nil, offset: CGPoint(x: 8, y: 8))
        
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 8 ))
        
//        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: 8))
        
//        pictureViewHeightCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        pictureViewWidthCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
        
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -8, y: 8))
        
        
//        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
    }
    
    
    //MARK: - 懒加载控件
    //顶部视图
    private lazy var topView : XFTopview = XFTopview()
    //微博文本内容
    lazy var contentLabel : UILabel = {
       let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.textColor = UIColor.darkGrayColor()
        
        label.numberOfLines = 0
        
        return label
    }()
    //底部视图
    lazy var bottomView : XFStatusBottomView = XFStatusBottomView()
    //配图视图
    lazy var pictureView : XFStatusPictureView = XFStatusPictureView()
    

}
